<?php

namespace app\admin\controller;

use app\admin\model\NoticeNode;
use think\Request;

class HomNode
{
    /***首页加载
     * @return \think\response\View
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $nodes = NoticeNode::all();
//        return $nodes;

        return view('HomNode/index', compact('nodes'));
    }

    /***加载新增分类页面
     * @return \think\response\View
     */

    public function create()
    {
        return view('HomNode/create');
    }


    /***保存新增
     * @param Request $request
     * @return \think\response\Redirect
     */
    public function save(Request $request)
    {
        NoticeNode::create($request->param());
        return redirect('HomNode/index');

    }

    /***加载编辑页面
     * @param $id
     * @return \think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function edit($id)
    {
        $node = NoticeNode::find($id);
        return view('HomNode/edit', compact('node'));
    }

    /***保存编辑
     * @param Request $request
     * @param $id
     * @return \think\response\Redirect
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function update(Request $request, $id)
    {
        $node = NoticeNode::find($id);
        $node->allowField(true)->save($request->param(), ['id' => $id]);
        return redirect('HomNode/index');
    }

    public function delete($id)
    {
        NoticeNode::destroy($id);
        return redirect('HomNode/index');
    }


}